<?php
 
class pStandard_Select extends cFormElement 
{	
	function getEditText($fieldName, $id, $value, cView $view)
	{
		$choices = $this->prepareChoices($view);
		if ($this->getAttr("style")!="radio")
		{
			$s.="<fieldset class=\"select\"><legend><label for=\"$id\">".$this->getAttr("label")."</label></legend>";
			$s.="<select name=\"$fieldName\" class=\"$name\">";
			foreach ($choices as $ch) {
				if ($value==$ch['val']) {
					$sel="selected=\"selected\""; 
				} else $sel="";
				$s.="<option value=\"".$ch['val']."\" $sel>".$ch['title']."</option>";
			}
			$s.="</select></fieldset>";
		} else {
			$s.="<fieldset class=\"select\"><legend><label for=\"$id\">".$this->getAttr("label")."</label></legend>";
			foreach ($choices as $key=>$ch) {
				if (($value==$ch['val']) || ($value=="" && $key==0)) {
					$sel="checked=\"checked\""; 
				} else $sel="";	
				$s.="<input type=\"radio\" name=\"$fieldName\" value=\"".$ch['val']."\" $sel id=\"".$id."_".$ch['val']."\" /><label for=\"".$id."_".$ch['val']."\" />".$ch['title']."</label><br />";
			}
			$s.="</fieldset>";
		}
		return $s;
	}
	
	function prepareChoices($view)
	{
		$ch = array();
		$keywords = array("children", "subnodes");
		if (in_array($rel=$this->getAttr("between"),$keywords))
		{
			if ($rel=="children") {
				$choices = $this->node->getChildren($view);
			} else if ($rel=="subnodes") {	
				$choices = $this->node->getSubNodes($view);
			}	
			foreach ($choices as $choice) {
				$ch[]=array("title"=>$choice->name,"val"=>$choice->id);
			}		} else {
			$choices = explode(",",$this->getAttr("between"));
			foreach ($choices as $k=>$choice) {
				$chunks=explode(":",$choice);
				$ch[] = array("title"=>$chunks[1],"val"=>$chunks[0]);
			}
		}		
		if (($e=$this->getAttr("empty"))!="") {
			$ch=array_reverse($ch);
			$ch[]=array("title"=>$e, "val"=>"NULL");
			$ch=array_reverse($ch);
		}			
		return $ch; 
	}
	
}
 
?>